// relate.cpp
// Customizer
// Protea Customizer / Business Logic for managing database relations (keys)
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"

//////////////////////////////////////////////////////////////////////////////
//
// Transactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 2
//@ CHA Add DB Relation
//@ COD AddDBRelation
//@ NUM 34
//@ CRI FORMAT_NAME
//@ CRI FIELD_NAME
//@ CRI RELATE_TYPE
//@ TAB DB_RELATE Write
//
//@ GEN Add DB Relation will add a relation to an application table
//
TRANSACTION_CODE( AddDBRelation )
{
	XCBuffer* buf = NULL;
	//@ GRS Start the transaction
	START_TXN( AddDBRelation );

		buf = App->CreateBuffer( FMT_DB_RELATE );
		buf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			//@ SUB DBRelateVerify to verify that this relation can be added
			DBRelateVerify( buf, &txnvars, VERIFY_ADD );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB DBRelateAdd to add the new relation
			DBRelateAdd( buf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

		txnvars.buffer->MoveCorresponding( buf, txnvars.Errors );
		App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

	CLEANUP_TXN();
		App->CleanupObjects( 1, &buf );
	END_TXN();
	//@ GRE End the transaction
};




//@ SEC 2
//@ CHA Delete DB Relation
//@ COD DeleteDBRelation
//@ NUM 35
//@ CRI FORMAT_NAME
//@ CRI FIELD_NAME
//@ CRI RELATE_TYPE
//@ TAB DB_RELATE Write
//
//@ GEN Delete DB Relation will remove a relation from an application table
//
TRANSACTION_CODE( DeleteDBRelation )
{
	XCBuffer* buf = NULL;

	//@ GRS Start the transaction
	START_TXN( DeleteDBRelation );

		buf = App->CreateBuffer( FMT_DB_RELATE );
		buf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			//@ SUB DBRelateVerify to verify that this relation can be deleted.
			DBRelateVerify( buf, &txnvars, VERIFY_DELETE );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB DBRelateDelete to delete the relation.
			DBRelateDelete( buf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &buf );
	END_TXN();
	//@ GRE End the transaction
};

